<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑模板'">
    <template #body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">名称</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.name" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">编码</label>
          </div>
          <div class="d-form-el d-relative">
            <el-tabs v-model="activeTpl">
              <el-tab-pane label="模板代码" name="first">
                <Coder v-model="state.tpl" ref="tpl" :height="'560px'" language="html" />
              </el-tab-pane>
              <el-tab-pane label="配置数据" name="second">
                <Coder v-if="activeTpl === 'second'" v-model="state.data" ref="data" :height="'560px'" language="javascript" />
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">预览</label>
          </div>
          <div class="d-form-el">
            <el-tabs v-model="activeCode">
              <el-tab-pane label="生成结果" name="first">
                <Coder v-model="code" ref="code" :height="'560px'" language="html" />
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">描述</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.description" class="d-form-textarea" rows="6"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">状态</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.status" class="d-form-select">
              <option v-for="item in statusList" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">排序</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.order" class="d-form-input" type="text" placeholder="数值越大越靠前" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">标签</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.labels" class="d-form-input" type="text" />
          </div>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button @click="handleRender" class="d-btn d-btn-default" type="button">生成</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages.js'
import { Dialoger, FormCalendar, FormSelecter, Tabs, TabsItem } from '@daelui/vdog/dist/components.js'
import { Coder } from '@daelui/vdog/dist/coder.js'
import { zh as locale } from '@daelui/dogjs/dist/locale.js'
import { objecter, parser } from '@daelui/dogjs/dist/components.js'
import template from '@daelui/dogjs/dist/libs/template'
import { STATUS } from '@daelui/dogjs/dist/constant.js'
import { state } from '../../../service/stock/index.js'
import tplSer from '../../../service/action/viewtpl.action'

export default {
  components: {
    PanerForm,
    FormCalendar,
    Dialoger,
    FormSelecter,
    Coder,
    Tabs, TabsItem
  },

  data () {
    return {
      STATUS,
      isEdit: false, // 是否是编辑模式
      activeTpl: 'first',
      activeCode: 'first',
      activeView: 'html',
      viewComp: 'div',
      isShowTab: true,
      // 表单数据
      state: {
        id: '',
        name: '',
        tpl: '',
        data: '{\n\n}',
        description: '',
        status: 1,
        order: 1,
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      code: '',
      // 状态列表
      statusList: state.statusList
    }
  },

  methods: {
    /**
     * @function 加载数据
    */
    loadItem (id) {
      tplSer.queryItem({ id }).then(result => {
        let data = result.data || {}
        this.state = data
        this.handleRender()
      })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      let state = objecter.clone(this.state)
      delete this.state.createTime
      // 编辑
      if (this.isEdit) {
        defer = tplSer.editItem(state)
      } else { // 添加
        defer = tplSer.addItem(state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
          // 添加则回到列表页
          if (!this.isEdit) {
            this.$router.push(this.getMicroRoute('/dview/tpl/list'))
          }
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    },

    /**
     * @function 重置
    */
    handleReset () {
      this.state = {
        id: '',
        name: '',
        tpl: '',
        data: '{\n\n}',
        description: '',
        status: 1,
        order: 1,
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      }
    },

    // 执行渲染
    handleRender () {
      let data = parser.parse(this.state.data)
      if (typeof data === 'object') {
        this.renderTpl(data)
      }
    },
    // 渲染模板
    renderTpl (data) {
      this.code = template.render(this.state.tpl, data, {
        tagStart : '{%',
        tagEnd : '%}',
        isReplaceWrap: false
      })
      this.$refs.code.monacoEditor.setValue(this.code)
    }
  },

  beforeMount () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
  }
}
</script>

<style lang="less" scoped>
.d-form-label {
  width: 90px;
}
.coder-paner {
  /deep/.d-paner-item-content {
    height: 500px;
  }
}
.coder-preview {
  overflow: auto;
  padding: 8px;
  height: 536px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
}
.btn-run {
  position: absolute;
  right: 0;
  top: 0;
}
</style>
<style lang="less">
.d-form-product .d-editor-container {
  height: 442px;
  .quill-editor {
    height: 400px
  }
}
</style>
